#ifndef __ELAPSEDTIMER_H__
#define __ELAPSEDTIMER_H__

#include <iostream>
#include <boost/noncopyable.hpp>
#include <boost/timer.hpp>
#include "assert.h"

class ElapsedTimer : public boost::timer
                   , private boost::noncopyable
{
public:
    ElapsedTimer(const std::string &msg, std::ostream &os = std::cout)
        : m_msg(msg), m_os(os)
    {}

    ~ElapsedTimer() {
        try {
            // use istream instead of ios_base to workaround GNU problem (Greg Chicares)
            std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
                                                          std::istream::floatfield );
            std::streamsize old_prec = m_os.precision(6);
            m_os << m_msg << " : "
                 << elapsed() << " s" // "s" is System International d'Unites std
                 << std::endl;
            m_os.flags( old_flags );
            m_os.precision( old_prec );
            m_os.flush();
        } catch (...) {
            // eat any exceptions
        }
    }

private:
    std::string m_msg;
    std::ostream & m_os;
};

#define ELAPSED_TIMER   ElapsedTimer __elapsed_timer(__ASSERT_FUNCTION);

#endif // __ELAPSEDTIMER_H__
